import PageConst from "./PageConst";
import { Message } from "uxcore";
import ReactEcharts from "echarts-for-react";

export default {
  defaults(props) {
    return {
      wHeight: "100vh",
      mHeight: 920 / 3,
      aHeight: 250,
      // 企业/人员基础信息
      oeq: {
        org: {
          a: "0",
          n: "0",
        },
        emp: {
          a: "0",
          n: "0",
        },
      },
      ...PageConst,
      // 招标中工程数量
      zbec: { k: "招标中", v: "0" },
      // 在建工程数量
      zjec: { k: "在建工程", v: "0" },
      // 竣工工程数量
      jgec: { k: "竣工工程", v: "0" },
      // 复工工程数量
      fgec: { k: "复工工程", v: "0" },

      pgSum: 0,

      dialogTitle: "",
      showDialog: false,
      dialogWidth: 1050,
      showDialogLx: "",
      //展示人员派工表格
      showDispatchTable: "",

      // 企业诚信详情
      cioCredit: {
        visible: false,
        creditLevel: "",
        data: [],
        currentPage: 0,
        totalCount: 0,
        pageSize: 10,
      },
      // 人员诚信行详情
      personCredit: {
        visible: false,
        creditLevel: "",
        data: [],
        currentPage: 0,
        totalCount: 0,
        pageSize: 10,
      },
      //工程派工人员信息
      dispatchSum: {
        loading: false,
        // visible: false,
        data: [],
        currentPage: 0,
        totalCount: 0,
        pageSize: 10,
      },
      //派工人员列表
      personList: {
        loading: false,
        sgxkId: "",
        visible: false,
        data: [],
        currentPage: 0,
        totalCount: 0,
        pageSize: 10,
      },

      dispatchRecord: {
        data: [],
        currentPage: 0,
        totalCount: 0,
        pageSize: 10,
      },

      moneyIssue: {
        data: [],
        currentPage: 0,
        totalCount: 0,
        pageSize: 2,
      },

      enterRecord: {
        loading: false,
        data: [],
        currentPage: 1,
        totalCount: 0,
        pageSize: 2,
      },

      //人员评价
      rypjData: {
        data: [],
        currentPage: 0,
        totalCount: 0,
        pageSize: 2,
      },

      //派工人员明细
      personDetails: {
        xq: {},
        pxjl: {},
        wj: [],
        kssj: {},
        //照片放大
        imgShow: {
          previewImage: "",
          visible: false,
        },
        visible: false,
      },
    };
  },

  //显示明细弹窗
  showModal(ctx) {
    ctx.setState(
      React.addons.update(ctx.getState(), {
        showDialog: { $set: true },
      })
    );
  },

  // 显示企业诚信详情
  showCioCreditModal(ctx) {
    ctx.setState(
      React.addons.update(ctx.getState(), {
        cioCredit: {
          visible: {
            $set: true,
          },
        },
      })
    );
  },
  // 显示企业诚信详情
  showPersonCreditModal(ctx) {
    ctx.setState(
      React.addons.update(ctx.getState(), {
        personCredit: {
          visible: {
            $set: true,
          },
        },
      })
    );
  },

  //隐藏明细弹窗
  hideModal(ctx) {
    ctx.setState(
      React.addons.update(ctx.getState(), {
        showDialog: { $set: false },
      })
    );
  },

  //隐藏图片放大窗口
  hideImgModal(ctx) {
    ctx.setState(
      React.addons.update(ctx.getState(), {
        personDetails: {
          imgShow: {
            visible: { $set: false },
          },
        },
      })
    );
  },

  // 隐藏企业诚信详情
  hideCioCreditModal(ctx) {
    ctx.setState(
      React.addons.update(ctx.getState(), {
        cioCredit: {
          visible: { $set: false },
          creditLevel: { $set: "" },
          data: { $set: [] },
          currentPage: { $set: 0 },
          totalCount: { $set: 0 },
          pageSize: { $set: 10 },
        },
      })
    );
  },

  // 隐藏人员诚信详情
  hidePersonCreditModal(ctx) {
    ctx.setState(
      React.addons.update(ctx.getState(), {
        personCredit: {
          visible: { $set: false },
          creditLevel: { $set: "" },
          data: { $set: [] },
          currentPage: { $set: 0 },
          totalCount: { $set: 0 },
          pageSize: { $set: 10 },
        },
      })
    );
  },

  //隐藏派工人员明细
  hideDispatchModal(ctx) {
    ctx.setState(
      React.addons.update(ctx.getState(), {
        dispatchSum: {
          loading: false,
          // visible: {$set: false},
          data: { $set: [] },
          currentPage: { $set: 0 },
          totalCount: { $set: 0 },
          pageSize: { $set: 10 },
        },
      })
    );
  },

  //隐藏派工人员列表
  hidePersonList(ctx) {
    ctx.setState(
      React.addons.update(ctx.getState(), {
        personList: {
          loading: { $set: false },
          visible: { $set: false },
          data: { $set: [] },
          currentPage: { $set: 0 },
          totalCount: { $set: 0 },
          pageSize: { $set: 10 },
        },
      })
    );
  },

  showPersonDetails(ctx) {
    ctx.setState(
      React.addons.update(ctx.getState(), {
        personDetails: {
          visible: { $set: true },
        },
      })
    );
  },

  //隐藏人员明细
  hidePersonDetails(ctx) {
    const personDetails = {
      xq: {},
      pxjl: {},
      wj: [],
      kssj: {},
      //照片放大
      imgShow: {
        previewImage: "",
        visible: false,
      },
      visible: false,
    };
    const dispatchRecord = {
      data: [],
      currentPage: 0,
      totalCount: 0,
      pageSize: 10,
    };
    const moneyIssue = {
      data: [],
      currentPage: 0,
      totalCount: 0,
      pageSize: 2,
    };

    const enterRecord = {
      data: [],
      currentPage: 1,
      totalCount: 0,
      pageSize: 2,
    };

    const rypjData = {
      data: [],
      currentPage: 0,
      totalCount: 0,
      pageSize: 2,
    };

    ctx.setState(
      React.addons.update(ctx.getState(), {
        personDetails: { $set: personDetails },
        dispatchRecord: { $set: dispatchRecord },
        moneyIssue: { $set: moneyIssue },
        enterRecord: { $set: enterRecord },
        rypjData: { $set: rypjData },
      })
    );
  },

  //照片弹出窗口
  async imgShow(ctx, params) {
    ctx.setState(
      React.addons.update(ctx.getState(), {
        personDetails: {
          imgShow: {
            visible: { $set: true },
            previewImage: { $set: params.url },
          },
        },
      })
    );
  },

  async fetchCioCredit(ctx, params) {
    const cioCredit = await ctx.fn.DB.elastic.search
      .ciocredit({
        creditLevel: params.creditLevel,
        pageSize: params.pageSize,
        currentPage: params.currentPage,
        Authorization: ctx.getProps().token,
      })
      .catch((error) => {
        console.log(error);
      });

    ctx.setState(
      React.addons.update(ctx.getState(), {
        cioCredit: {
          creditLevel: { $set: params.creditLevel },
          data: { $set: cioCredit.content },
          currentPage: { $set: cioCredit.number + 1 },
          totalCount: { $set: cioCredit.totalElements },
          pageSize: { $set: cioCredit.size },
        },
      })
    );
  },
  async fetchDispatchSum(ctx, params) {
    ctx.setState(
      React.addons.update(ctx.getState(), {
        dispatchSum: {
          loading: { $set: true },
        },
      })
    );
    const content = await ctx.fn.DB.basic.info
      .pgList({
        pageSize: params.pageSize,
        currentPage: params.currentPage,
        Authorization: ctx.getProps().token,
      })
      .catch((error) => {
        console.log(error);
      });
    ctx.setState(
      React.addons.update(ctx.getState(), {
        dispatchSum: {
          loading: { $set: false },
          data: {
            $set: content.records,
          },
          currentPage: { $set: content.current },
          totalCount: { $set: content.total },
          pageSize: { $set: content.size },
        },
      })
    );
  },
  async pgjl(ctx, params) {
    const content = await ctx.fn.DB.basic.info
      .pgjl({
        personId: params.xq.personId,
        Authorization: ctx.getProps().token,
      })
      .catch((error) => {
        console.log(error);
      });
    ctx.setState(
      React.addons.update(ctx.getState(), {
        dispatchRecord: {
          data: {
            $set: content.data,
          },
        },
      })
    );
  },

  // 查询人员评价
  async rypj(ctx, params) {
    const content = await ctx.fn.DB.basic.info
      .rypj({
        current: params.currentPage,
        size: params.pageSize,
        laborPersonId: params.xq.personId,
        Authorization: ctx.getProps().token,
      })
      .catch((error) => {
        console.log(error);
      });
    ctx.setState(
      React.addons.update(ctx.getState(), {
        rypjData: {
          data: {
            $set: content.data.records,
          },
          currentPage: { $set: content.data.current },
          totalCount: { $set: content.data.total },
          pageSize: { $set: 2 },
        },
      })
    );
  },

  async gz(ctx, params) {
    const content = await ctx.fn.DB.basic.info
      .gz({
        idCardNumber: params.xq.idCardNumber,
        //      engName:params.xq.sgxkName,
        limit: params.pageSize,
        page: params.currentPage,
        Authorization: ctx.getProps().token,
      })
      .catch((error) => {
        console.log(error);
      });

    ctx.setState(
      React.addons.update(ctx.getState(), {
        moneyIssue: {
          data: {
            $set: content.records,
          },
          currentPage: { $set: content.current },
          totalCount: { $set: content.total },
          pageSize: { $set: content.size },
        },
      })
    );
  },

  // 人员进出记录
  async jcjl(ctx, params) {
    ctx.setState(
      React.addons.update(ctx.getState(), {
        personDetails: {
          visible: { $set: true },
        },
        enterRecord: {
          loading: { $set: true },
        },
      })
    );
    const content = await ctx.fn.DB.basic.info
      .jcjl({
        idCardNumber: params.xq.idCardNumber,
        limit: params.pageSize,
        page: params.currentPage,
        Authorization: ctx.getProps().token,
      })
      .catch((error) => {
        console.log(error);
      });

    if (content) {
      ctx.setState(
        React.addons.update(ctx.getState(), {
          personDetails: {
            visible: { $set: true },
          },
          enterRecord: {
            loading: { $set: false },
            data: {
              $set: content.records,
            },
            currentPage: { $set: content.current || 1 },
            totalCount: { $set: content.total || 0 },
            pageSize: { $set: content.size || 2 },
          },
        })
      );
    } else {
      const { enterRecord } = ctx.getState();
      enterRecord.loading = false;
      Message['error']('连接超时，请重试！');
      ctx.setState(
        React.addons.update(ctx.getState(), {
          personDetails: {
            visible: { $set: true },
          },
          enterRecord: { $set: enterRecord },
        })
      );
    }
  },

  //派工单个人员明细
  async pxjl(ctx, params) {
    const content = await ctx.fn.DB.basic.info
      .pxjl({
        idCardNumber: params.xq.idCardNumber,
        Authorization: ctx.getProps().token,
      })
      .catch((error) => {
        console.log(error);
      });

    if (content && content.records && content.records[0]) {
      // 获取文件
      const wj = await ctx.fn.DB.basic.info
        .wj({
          glid: content.records[0].id,
          Authorization: ctx.getProps().token,
        })
        .catch((error) => {
          console.log(error);
        });
      ctx.setState(
        React.addons.update(ctx.getState(), {
          personDetails: {
            wj: { $set: wj.data },
          },
        })
      );

      // 获取考试试卷
      const { id, idCardNumber } = content.records[0];
      const kssj = await ctx.fn.DB.basic.info
        .kssj({
          trainPlanRecordId: id,
          idCardNumber: idCardNumber,
        })
        .catch((error) => {
          console.log(error);
        });
      ctx.setState(
        React.addons.update(ctx.getState(), {
          personDetails: {
            kssj: { $set: kssj },
          },
        })
      );
    }

    ctx.setState(
      React.addons.update(ctx.getState(), {
        personDetails: {
          xq: { $set: params.xq },
          pxjl: {
            $set:
              content && content.records && content.records[0]
                ? content.records[0]
                : {},
          },
        },
      })
    );
  },

  //查询派工人员列表
  async fetchPersonList(ctx, params) {
    ctx.setState(
      React.addons.update(ctx.getState(), {
        personList: {
          loading: { $set: true },
          visible: { $set: true },
        },
      })
    );
    const content = await ctx.fn.DB.basic.info
      .personList({
        sgxkId: params.sgxkId,
        pageSize: params.pageSize,
        currentPage: params.currentPage,
        Authorization: ctx.getProps().token,
      })
      .catch((error) => {
        console.log(error);
      });

    if (content) {
      ctx.setState(
        React.addons.update(ctx.getState(), {
          personList: {
            loading: { $set: false },
            sgxkId: { $set: params.sgxkId },
            data: {
              $set: content.records,
            },
            currentPage: { $set: content.current },
            totalCount: { $set: content.total },
            pageSize: { $set: content.size },
          },
        })
      );
    } else {
      ctx.setState(
        React.addons.update(ctx.getState(), {
          personList: {
            loading: { $set: false },
            data: {
              $set: [],
            },
            currentPage: { $set: 1 },
            totalCount: { $set: 0 },
            pageSize: { $set: 0 },
          },
        })
      );
    }
  },

  async fetchPersonCredit(ctx, params) {
    const personCredit = await ctx.fn.DB.elastic.search
      .personcredit({
        creditLevel: params.creditLevel,
        pageSize: params.pageSize,
        currentPage: params.currentPage,
        Authorization: ctx.getProps().token,
      })
      .catch((error) => {
        console.log(error);
      });

    ctx.setState(
      React.addons.update(ctx.getState(), {
        personCredit: {
          creditLevel: { $set: params.creditLevel },
          data: { $set: personCredit.content },
          currentPage: { $set: personCredit.number },
          totalCount: { $set: personCredit.totalElements },
          pageSize: { $set: personCredit.size },
        },
      })
    );
  },

  // 弹出框内容
  async viewMx(ctx, lx, params) {
    let t = this;
    switch (lx) {
      case "rysl":
        const ryslOption = {
          tooltip: {
            trigger: "item",
            formatter: "{a} <br/>{b}: {c} ({d}%)",
          },
          legend: {
            orient: "vertical",
            x: "right",
            align: "left",
            top: "40",
            padding: [0, 10, 0, 0],
            formatter: (name) => {
              return name;
            },
            data: ["管理人员", "非管理人员"],
          },
          color: ["#B5C334", "#27727B"],
          series: [
            {
              name: "人员类型",
              type: "pie",
              // radius: ['45%', '70%'],
              // center: ['34%', '40%'],
              avoidLabelOverlap: false,
              formatter: "{a|{a}}{abg|}\n{hr|}\n  {b|{b}：}{c}{per|{d}%}",
              label: {
                normal: {
                  show: false,
                  position: "center",
                },
                emphasis: {
                  show: false,
                  textStyle: {
                    fontSize: "16",
                    fontWeight: "bold",
                  },
                },
              },
              labelLine: {
                normal: {
                  show: true,
                },
              },
              data: [
                { name: "管理人员", value: 19680 },
                { name: "非管理人员", value: 21970 },
              ],
            },
          ],
        };
        ctx.setState(
          React.addons.update(ctx.getState(), {
            dialogTitle: { $set: "人员数量分类占比" },
            dialogWidth: { $set: 600 },
            showDialogLx: {
              $set: <ReactEcharts id="rysl" option={ryslOption} />,
            },
          })
        );
        break;

      case "zsfl":
        const { ryzsTypeOption } = ctx.getState();
        ctx.setState(
          React.addons.update(ctx.getState(), {
            dialogTitle: { $set: "人员证书分类柱状图" },
            dialogWidth: { $set: 1080 },
            showDialogLx: {
              $set: <ReactEcharts id="zsfl" option={ryzsTypeOption} />,
            },
          })
        );
        break;
      default:
        break;
    }
  },

  // 复工数量查询
  async fgeng(ctx, props) {
    const content = await ctx.fn.DB.basic.info.map().catch((error) => {
      console.log(error);
    });

    if (content) {
      ctx.setState(
        React.addons.update(ctx.getState(), {
          fgec: { $set: { k: "复工工程", v: content.length + "" } },
        })
      );
    }
  },

  // 企业/人员基础信息查询
  async oeq(ctx, props) {
    const content = await ctx.fn.DB.basic.info
      .oeq({ Authorization: ctx.getProps().token })
      .catch((error) => {
        console.log(error);
      });
    if (content) {
      ctx.setState(
        React.addons.update(ctx.getState(), {
          oeq: { $set: content },
        })
      );
    }
  },

  // 企业类型比例查询
  async oc(ctx, props) {
    const content = await ctx.fn.DB.basic.info
      .oc({ Authorization: ctx.getProps().token })
      .catch((error) => {
        console.log(error);
      });
    if (content) {
      let oc = {
        legend: [],
        series: {
          // 外圈
          data1: [],
          // 内圈
          data2: [],
        },
      };

      content.kv1.map((kv) => {
        if (kv.k in oc.legend) {
        } else {
          oc.legend.push(kv.k);
        }
        oc.series.data1.push({ value: kv.v, name: kv.k });
      });

      content.kv2.map((kv) => {
        oc.series.data2.push({ value: kv.v, name: kv.k });
      });

      ctx.setState({
        orgTypeOption: React.addons.update(ctx.getState().orgTypeOption, {
          legend: {
            data: { $set: oc.legend },
          },
          series: {
            $apply: (series) => {
              return series.map((item, index) => {
                if (index === 0) {
                  return React.addons.update(item, {
                    data: { $set: oc.series.data2 },
                  });
                } else if (index === 1) {
                  return React.addons.update(item, {
                    data: { $set: oc.series.data1 },
                  });
                }
              });
            },
          },
        }),
      });
    }
  },

  // 招标中工程项目数量查询,在建工程项目数量查询,竣工工程项目数量查询
  async gcec(ctx, props) {
    const content = await ctx.fn.DB.basic.info
      .gcec({ Authorization: ctx.getProps().token })
      .catch((error) => {
        console.log(error);
      });

    if (content) {
      content.map((kv) => {
        if (kv.k === "在建工程(全市)") {
          ctx.setState(
            React.addons.update(ctx.getState(), {
              zbec: { $set: kv },
            })
          );
        } else if (kv.k === "竣工工程(今年)") {
          ctx.setState(
            React.addons.update(ctx.getState(), {
              jgec: { $set: kv },
            })
          );
        } else if (kv.k === "在建工程(城区)") {
          ctx.setState(
            React.addons.update(ctx.getState(), {
              zjec: { $set: kv },
            })
          );
        }
      });
    }
  },

  // 人员证书分类数据查询
  async pcc(ctx, props) {
    const content = await ctx.fn.DB.basic.info
      .pcc({ Authorization: ctx.getProps().token })
      .catch((error) => {
        console.log(error);
      });
    if (content) {
      let pcc = {
        xAxis: [],
        series: [],
      };
      content.map((kv) => {
        pcc.xAxis.push(kv.k);
        pcc.series.push(kv.v);
      });

      ctx.setState({
        ryTypeption: React.addons.update(ctx.getState().ryTypeption, {
          xAxis: {
            $apply: (xAxis) => {
              return xAxis.map((item) => {
                return React.addons.update(item, { data: { $set: pcc.xAxis } });
              });
            },
          },
          series: {
            $apply: (series) => {
              return series.map((item) => {
                return React.addons.update(item, {
                  data: { $set: pcc.series },
                });
              });
            },
          },
        }),
      });
    }
  },

  // 人员证书分类数据查询（全部）
  async pccAll(ctx, props) {
    const content = await ctx.fn.DB.basic.info
      .pccall({ Authorization: ctx.getProps().token })
      .catch((error) => {
        console.log(error);
      });
    if (content) {
      let pcc = {
        xAxis: [],
        series: [],
      };
      content.map((kv) => {
        pcc.xAxis.push(kv.k);
        pcc.series.push(kv.v);
      });

      ctx.setState({
        ryzsTypeOption: React.addons.update(ctx.getState().ryzsTypeOption, {
          xAxis: {
            $apply: (xAxis) => {
              return xAxis.map((item) => {
                return React.addons.update(item, { data: { $set: pcc.xAxis } });
              });
            },
          },
          series: {
            $apply: (series) => {
              return series.map((item) => {
                return React.addons.update(item, {
                  data: { $set: pcc.series },
                });
              });
            },
          },
        }),
      });
    }
  },

  // 企业信用评级数据查询
  async ocr(ctx, props) {
    const content = await ctx.fn.DB.basic.info
      .ocr({ Authorization: ctx.getProps().token })
      .catch((error) => {
        console.log(error);
      });
    if (content) {
      content.map((kv) => {
        ctx.setState({
          ocrOptions: React.addons.update(ctx.getState().ocrOptions, {
            series: {
              $apply: (series) => {
                return series.map((item) => {
                  if (item.name === "2") {
                    return React.addons.update(item, {
                      data: {
                        $apply: (data) => {
                          return data.map((item2) => {
                            if (item2.name === kv.k) {
                              return React.addons.update(item2, {
                                value: {
                                  $set: kv.v,
                                },
                              });
                            } else {
                              return item2;
                            }
                          });
                        },
                      },
                    });
                  } else {
                    return item;
                  }
                });
              },
            },
          }),
        });
      });
    }
  },

  async engWorkerStatistical(ctx, props) {
    const content = await ctx.fn.DB.basic.info
      .engWorkerStatistical({
        Authorization: ctx.getProps().token,
        param: "1000",
      })
      .catch((error) => {
        console.log(error);
      });
    let pgSum = 0;
    if (content) {
      if (Array.isArray(content)) {
        const pgryCount = content.filter((item) => item.id === "1004");
        if (pgryCount.length > 0) {
          const { result } = pgryCount[0];
          pgSum = result;
        }
      }
    }
    ctx.setState(
      React.addons.update(ctx.getState(), {
        pgSum: { $set: pgSum },
      })
    );
  },

  // 务工人员派工
  async pg(ctx, props) {
    // const content = await ctx.fn.DB.basic.info.pg({Authorization: ctx.getProps().token}).catch(
    const content = await ctx.fn.DB.fgfc.xmry
      .count({ Authorization: ctx.getProps().token })
      .catch((error) => {
        console.log(error);
      });

    if (content) {
      ctx.setState(
        React.addons.update(ctx.getState(), {
          pgSum: { $set: content.fgryzs },
        })
      );
    }
  },

  // 个人信用评级数据查询
  async pcr(ctx, props) {
    const content = await ctx.fn.DB.basic.info
      .pcr({ Authorization: ctx.getProps().token })
      .catch((error) => {
        console.log(error);
      });

    if (content) {
      let pcr = {
        legend: [],
        series: [],
      };
      content.map((kv) => {
        // pcr.legend.push(kv.k);
        pcr.series.push({
          value: kv.v,
        });
      });
      ctx.setState({
        pcrOptions: React.addons.update(ctx.getState().pcrOptions, {
          series: {
            $apply: (series) => {
              return series.map((item) => {
                return React.addons.update(item, {
                  data: { $set: pcr.series },
                });
              });
            },
          },
        }),
      });
    }
  },
};
